package cn.edu.jxust.controller.api.site;

import cn.edu.jxust.common.domain.entity.Site;
import cn.edu.jxust.common.domain.dto.DeviceSiteVo;
import cn.edu.jxust.common.domain.dto.SiteAirTypeVo;
import cn.edu.jxust.common.domain.valobj.SiteAirTypeVoSimple;
import cn.edu.jxust.common.domain.dto.SiteVo;
import cn.edu.jxust.common.domain.web.api.site.ISiteController;
import cn.edu.jxust.common.response.Result;
import cn.edu.jxust.common.response.ResultUtils;
import cn.edu.jxust.common.response.enums.ResultStatus;
import cn.edu.jxust.core.domain.repository.DeviceService;
import cn.edu.jxust.core.domain.repository.SiteService;
import cn.edu.jxust.core.infrastructure.util.Const;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuguangyun
 * @since 2020-07-06
 */
@Slf4j
@RestController
public class SiteController implements ISiteController {

    @Resource
    private SiteService siteService;
    @Resource
    private DeviceService deviceService;

    @Override
    public Result addSite(SiteVo siteVo, BindingResult result){

        if(result.hasErrors() || siteVo.getCreateUsername() == null){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try {
            return siteService.addSite(siteVo)
                    ? ResultUtils.resultWithoutData(ResultStatus.SUCCESS, "监测点添加成功")
                    : ResultUtils.resultWithoutData(ResultStatus.FAILED, "监测点添加失败, 请重试!");
        } catch (Exception e) {
            log.error("[SiteController --> adSite]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result delSite(Integer id){

        try {
            return siteService.delSiteById(id);
        }catch (Exception e){
            log.error("[SiteController --> delSite]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result modifySite(SiteVo siteVo, BindingResult result){

        if(result.hasErrors() || siteVo.getId() == null || siteVo.getUpdateUsername() == null){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try {
            return siteService.modifySiteInfo(siteVo);
        } catch (Exception e) {
            log.error("[SiteController --> modifySite]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result getOneSite(Integer id){

        try{
            Site byId = siteService.getSiteById(id);
            if(byId == null){
                return ResultUtils.resultWithoutData(ResultStatus.NOT_FOUND,"该监测点信息不存在！");
            }else {
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, byId);
            }
        }catch (Exception e) {
            log.error("[SiteController --> getOneSite]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result getSiteList(Integer page, Integer size, HttpServletRequest request){

        try{
            Map<String, String[]> paramMap = request.getParameterMap();

            IPage<SiteVo> sitePage = siteService.getSitePage(paramMap, page, size);
            if(sitePage.getRecords() != null && !sitePage.getRecords().isEmpty()){
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, sitePage);
            }else {
                return ResultUtils.resultWithoutData(ResultStatus.NOT_FOUND, "未找到任何监测点信息");
            }
        }catch (Exception e){
            log.error("[SiteController --> getSiteList]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result getNoBinding(Integer page, Integer size){

        try{
            IPage<Site> sitePage = siteService.getNoBindingSite(page, size);
            if(sitePage.getRecords() != null && !sitePage.getRecords().isEmpty()){
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, sitePage);
            }else {
                return ResultUtils.resultWithoutData(ResultStatus.NOT_FOUND, "未找到任何监测点信息");
            }
        }catch (Exception e){
            log.error("[SiteController --> getNoBinding]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result binding(DeviceSiteVo deviceSiteVo, BindingResult result) {

        if(result.hasErrors()){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try{
            if(deviceSiteVo.getIsBinding().equals(Const.BINDING)){
                //监测点绑定设备操作

                //判断监测点是否绑定
                if(siteService.siteIsBinding(deviceSiteVo.getSiteId())){
                    //监测点已经绑定了设备
//                    log.error("该监测点已经绑定了监测设备, 请更换监测点或先解绑!");
                    return ResultUtils.resultWithoutData(ResultStatus.FAILED, "该监测点已经绑定了监测设备, 请更换监测点或先解绑!");
                }

                //判断设备是否绑定
                if(deviceService.deviceIsBinding(deviceSiteVo.getDeviceId())){
                    //设备已经绑定了监测点
//                    log.error("该设备已经绑定了监测点, 请更换设备或先解绑!");
                    return ResultUtils.resultWithoutData(ResultStatus.FAILED, "该设备已经绑定了监测点, 请更换设备或先解绑!");
                }

            }else if(deviceSiteVo.getIsBinding().equals(Const.NOT_BINDING)){
                //监测点解绑设备操作

                //判断监测点是否绑定
                if(!siteService.siteIsBinding(deviceSiteVo.getSiteId())){
                    //监测点没有绑定设备
//                    log.error("该监测点没有绑定监测设备, 请查验正确!");
                    return ResultUtils.resultWithoutData(ResultStatus.FAILED, "该监测点没有绑定监测设备, 请查验正确!");
                }

                //判断设备是否绑定
                if(!deviceService.deviceIsBinding(deviceSiteVo.getDeviceId())){
                    //设备没有绑定监测点
//                    log.error("该设备未绑定该监测点, 请查验正确!");
                    return ResultUtils.resultWithoutData(ResultStatus.FAILED, "该设备未绑定该监测点, 请查验正确!");
                }

                //绑定了设备, 判断绑定的监测点的id是否正确
                if(!deviceSiteVo.getSiteId().equals(deviceService.getSiteIdById(deviceSiteVo.getDeviceId()))){
                    //并没有绑定该监测点
//                    log.error("该监测点未绑定此设备, 请查验正确!");
                    return ResultUtils.resultWithoutData(ResultStatus.FAILED, "该监测点未绑定此设备, 请查验正确!");
                }
            }

            String msg = deviceSiteVo.getIsBinding().equals(Const.NOT_BINDING) ? "解绑" : "绑定";
            return siteService.bindingDevice(deviceSiteVo)
                    ? ResultUtils.resultWithoutData(ResultStatus.SUCCESS, "监测点" + msg + "设备成功")
                    : ResultUtils.resultWithoutData(ResultStatus.FAILED, "监测点" + msg + "设备失败");
        }catch (Exception e){
            log.error("DeviceController --> binding: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result getAllSiteAirType(Integer siteId, Integer page, Integer size){

        try{
            IPage<SiteAirTypeVo> pageInfo = siteService.getSiteAirTypeVoListBySiteId(page, size, siteId);
            if(null != pageInfo.getRecords() && !pageInfo.getRecords().isEmpty()){
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, pageInfo);
            }else {
                return ResultUtils.resultWithoutData(ResultStatus.NOT_FOUND, "未找到任何监测气体信息");
            }
        }catch (Exception e){
            log.error("SiteAirTypeController --> getAllSiteAirType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result addOrdelSiteAirType(SiteAirTypeVo siteAirTypeVo, BindingResult result){

        if(result.hasErrors()){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try{
            return siteService.addOrdelSiteAirType(siteAirTypeVo);
        }catch (Exception e){
            log.error("SiteAirTypeController --> addOrdelSiteAirType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result getSiteAirListByDeviceId(Integer deviceId){

        try{
            List<SiteAirTypeVoSimple> list = siteService.getSiteAirTypeByDeviceId(deviceId);
            if(null != list && list.size() != 0){
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, list);
            }else{
                return ResultUtils.resultWithoutData(ResultStatus.NOT_FOUND, "未找到任何监测气体列表");
            }
        }catch (Exception e){
            log.error("SiteAirTypeController --> getSiteAirListByDeviceId: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

}


